package com.hkedou.yuanhuo.shiro.redis.impl;

import com.hkedou.yuanhuo.adminsystem.business.user.model.SysUser;
import com.hkedou.yuanhuo.shiro.redis.IRedisManager;
import com.hkedou.yuanhuo.shiro.redis.RedisConf;
import com.hkedou.yuanhuo.shiro.redis.RedisTokenManager;
import com.hkedou.yuanhuo.token.serializer.ObjectSerializer;
import com.hkedou.yuanhuo.token.serializer.RedisSerializer;
import com.hkedou.yuanhuo.token.serializer.SerializationException;
import com.hkedou.yuanhuo.token.serializer.StringSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Component
public class RedisTokenManagerImpl implements RedisTokenManager {


    private static ThreadLocal<Map<String,Object>> userInThread = new ThreadLocal<>();


    Logger logger= LoggerFactory.getLogger(RedisTokenManagerImpl.class);

    private IRedisManager redisManager;

    private RedisSerializer keySerializer = new StringSerializer();
    private RedisSerializer valueSerializer = new ObjectSerializer();

    @Resource
    private RedisConf redisConf;

    public IRedisManager getRedisManager() {
        return redisManager;
    }

    public void setRedisManager(IRedisManager redisManager) {
        this.redisManager = redisManager;
    }

    @Override
    public String helloasus() {
        return "hello asus";
    }

    @Override
    public SysUser tokenUser(String token) {
        try {
            byte[] bytes = redisManager.get(keySerializer.serialize((token)));
            if(bytes!=null){
                SysUser sysUser = (SysUser) valueSerializer.deserialize(bytes);
                return sysUser;
            }

        } catch (SerializationException e) {
            logger.error("serialize token error. token="+token,token,e);
        }
        return null;
    }

    @Override
    public void delToken(String token) {

        try {
            redisManager.del(keySerializer.serialize((token)));
        } catch (SerializationException e) {
            logger.error("del token error. token="+token,token,e);
        }
    }

    @Override
    public void addToken(String token, Object obj) {
        byte[] key;
        byte[] value;
        try {
            key = keySerializer.serialize((token));

            value = valueSerializer.serialize(obj);

            redisManager.set(key,value,redisConf.getTimeout());
        } catch (SerializationException e) {
            logger.error("serialize token error. token="+token,token,e);
        }
    }





    public static Map<String, Object> getUserMap() {
        return userInThread.get();
    }

    public static Object getUserValue(String key) {
        if(userInThread.get()==null){
           return null;
        }
        return userInThread.get().get(key);
    }

    public static void setUserValue(String key,Object value) {
        Map<String, Object> userMap = userInThread.get();
        if(userMap ==null){
            userMap=new HashMap<>();
            userInThread.set(userMap);
        }
        userMap.put(key,value);
    }
}
